/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.gemfire.examples;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.examples.domain.Order;
import org.springframework.stereotype.Repository;

import org.apache.geode.cache.query.SelectResults;

/**
 * An implementation of {@link OrderDao} using {@link GemfireTemplate}
 * @author David Turanski
 *
 */
@Repository
public class GemfireTemplateOrderDao implements OrderDao {
	@Autowired
	GemfireTemplate orderTemplate;

	/* (non-Javadoc)
	 * @see org.springframework.data.gemfire.examples.OrderDao#findCustomerOrders(long)
	 */
	@Override
	public List<Order> findCustomerOrders(long customerId) {
		SelectResults<Order> orders = orderTemplate.find("SELECT * from /Order WHERE customerId = $1", customerId);
		return (orders == null)?null : orders.asList();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.gemfire.examples.OrderDao#delete(long)
	 */
	@Override
	public void delete(long id) {
		orderTemplate.remove(id);
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.gemfire.examples.OrderDao#save(org.springframework.data.gemfire.examples.domain.Order)
	 */
	@Override
	public Order save(Order order) {
		return orderTemplate.put(order.getId(), order);
	}

	@Override
	public Order get(long id) {
		return orderTemplate.get(id);
	}
}
